<?
    class FilesController extends RepositoryController
    {
		protected $entity = 'StoredFile';
		
		public function get($id)
		{			
			$rep  = $this->repo();
			$file = $rep->get($id);
			
			header('Content-type: image/png');
			
			print $file['data'];
		}
		
		public function thumbnail($id)
		{
			$rep  = $this->repo();
			$file = $rep->get($id);
			$thumbnail = $file['thumbnail'];
			
			header('Content-type: image/gif');
			
			if(empty($thumbnail))
			{
				$thumbnail = ImageHelper::resize($file['data'], 100, 100);
				
				$rep->update(array('thumbnail' => addslashes($thumbnail)), $id);
			}
			
			print $thumbnail;
		}
		
		public function favorite($parent_type, $parent_id, $id)
		{
			$rep = $this->repo();
			$query = $rep->newQuery();
			$query->where(array(
						'eq' => array(
							array('parent_type', $parent_type),
							array('parent_id', $parent_id),
							array('favorite', true)
						)
					))
					->update(array('favorite' => false));
			
			
			$rep->update(array('favorite' => true), $id);
			
			return array('success' => true);
		}
		
		public function load($parent_type = null, $parent_id = null)
		{
			$rep = new Repository('StoredFile');
			$query = $rep->newQuery();
			$query->field('name')
				  ->field('file_id')
				  ->field('favorite')
				  ->field('parent_id')
				  ->field('parent_type');
			
			if(isset($parent_id) && isset($parent_type))
			{
				$query->where(array(
					'eq' => array(
						array('parent_id', $parent_id),
						array('parent_type', $parent_type)
					)
				));
			}
			
			$this->json(array(
				'success' => true,
				'data' => $query->results()
			));
		}
	
		public function upload($parent_type, $parent_id)
		{
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
			header("Cache-Control: no-store, no-cache, must-revalidate");
			header("Cache-Control: post-check=0, pre-check=0", false);
			header("Pragma: no-cache");
			
			$in = null;
			$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
			$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
			
			$contentType = isset($_SERVER["HTTP_CONTENT_TYPE"]) 
				? $_SERVER["HTTP_CONTENT_TYPE"]
				: $_SERVER["CONTENT_TYPE"];
			
			$multipart = strpos($contentType, "multipart") !== false;
			
			if($multipart){
				$file = $_FILES['file']['tmp_name'];
				
				if(isset($file) && is_uploaded_file($file)){
					$in = fopen($_FILES['file']['tmp_name'], "rb");
				}			
			} else {
				$in = fopen("php://input", "rb");
			}
			
			if($in){
				$bytes = stream_get_contents($in);
				
				$data = array(
					'parent_id' => $parent_id,
					'parent_type' => $parent_type,
					'name' => 'Новый файл',
					'data' => addslashes($bytes)
				);
				
				fclose($in);
				
				$rep = new Repository('StoredFile');
				
				if($rep->insert($data))
				{
					print "{success:true}";
				}
				else
				{
					print "{success:false, msg:'Не удалось записать файл на сервер.'}";
				}
				
				if($multipart){
					@unlink($_FILES['file']['tmp_name']);
				}
				
				exit;
			}
			
			print "{success:false, msg:'Не удалось прочитать файл.'}";
		}
    }
?>